
var app = getApp()

Page({
	data: {
		items: '',
		limit: 10,
		page: 1,
		done: false,
		keywords:'',
		showmenu1: true,
		showmenu2: true,
		showmenu3: true,
		showmenu4: true,
		searchlimit:5,
		searchbnt: false
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		this.getDataList();
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {
		wx.stopPullDownRefresh()
		// this.getDataList()
		// setTimeout(() => {
		// 	wx.showToast({
		// 		title: '刷新成功'
		// 	})
		// 	wx.stopPullDownRefresh()
		// }, 1000)

		return true;
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {
		this.getDataList()
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	},

	getDataList() {

		var that = this;
		var openId = wx.getStorageSync('user_new');
		var limit = that.data.limit || 10;
		var page = that.data.page || 1;
		var keywords = that.data.keywords || '';

		if (this.data.done) {

			wx.showToast({
				title: '暂无更多内容',
				duration:1000,
				icon:'none'
			})

			return false
		}

		wx.showLoading({
			title: '加载中',
		})

		app.core('get', 'hire/mypost/openId/' + openId +'/limit/'+limit+'?page=' + page + '&keywords=' + keywords, {},
			(res) => {

				var arr = res.data.items;

				if (arr.length == 0) {
					wx.showToast({
						title: '暂无更多内容',
						duration:1000,
						icon:'none'
					})

					if (page == 1) {

						that.setData({
							items: [],
						})
					}

					return false
				}

				for (var i = 0; i < arr.length; i++) {
					switch (arr[i].propertyType) {
						case 1:
							arr[i].propertyType = '住宅'
							break;
						case 2:
							arr[i].propertyType = '商铺'
							break;
						case 3:
							arr[i].propertyType = '写字楼'
							break;
						default:
							arr[i].propertyType = '住宅'
					}

					if (arr[i].fitment == 1) {
						arr[i].fitment = '毛坯'
					} else if (arr[i].fitment == 2) {
						arr[i].fitment = '普通装修'
					} else if (arr[i].fitment == 3) {
						arr[i].fitment = '精装修'
					} else {
						arr[i].fitment = '其他'
					}
				}

				var items = that.data.items || []

				if (page < res.data.lastPage ) {
					var _page = that.data.page + 1
				} else {
					var _page = 0;
					that.setData({
						done: true
					})
				}

				that.setData({
					items: [ ...items, ...arr],
					page: _page
				})

				wx.hideLoading()
			}
		)
	},// 二手楼盘

	searchChange(e) {
		this.setData({
			keywords: e.detail.value
		})
	},

	handleSearch() {
		this.setData({
			page: 1,
			done: false,
			items: [],
		})
		this.getDataList()
	},

	handleAdd() {
		wx.navigateTo({
			url: '/pages/hire/add/index'
		})
	},

	handleEdit() {
		return false
	},

	handleView(e) {
		console.log(e)
		return false
	},

	/**
	 * 删除绑定楼盘
	 */
	bindDelete(e){
		console.log(e)
		var index = e.currentTarget.dataset.index;
		var newhouse_project_id = e.currentTarget.dataset.id;
		console.log(newhouse_project_id)
		var houseList = this.data.items;
		var that = this;
		var openId = wx.getStorageSync('user_new');
		wx.showModal({
			title: '提示',
			content: '确认删除吗？',
			success(res) {
				if (res.confirm) {

					app.core('get', 'hire/mypost/delete/openId/' + openId +'/hId/'+newhouse_project_id, {}, (res) => {
						if (res.resultCode == 200) {
							houseList.splice(index, 1);
							that.setData({ items: houseList})
						}

						wx.showToast({
							title: res.msg,
							icon: 'none'
						})
					})

				} else if (res.cancel) {
					console.log('用户点击取消')
				}
			}
		})
	},
})
